<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        #result{
            width: 400px;
            height: 300px;
            border:solid 1px #555;
        }

        input{
            padding: 6px 12px;
            font-size: 14px;
            line-height: 1.42857143;
            color: #555;
            background-color: #fff;
            background-image: none;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%);
            transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
        }

        .active{
            border-color: #66afe9;
            outline: 0;
            box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%), 0 0 8px rgb(102 175 233 / 60%);
        }

        button{
            display: inline-block;
            margin-bottom: 0;
            font-weight: 400;
            text-align: center;
            white-space: nowrap;
            vertical-align: middle;
            -ms-touch-action: manipulation;
            touch-action: manipulation;
            cursor: pointer;
            background-image: none;
            border: 1px solid transparent;
            padding: 6px 12px;
            font-size: 14px;
            line-height: 1.42857143;
            border-radius: 4px;
            color: #fff;
            background-color: #337ab7;
            border-color: #2e6da4;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //声明一个 state
            state = {
                msgs: [
                ]
            }

            render(){
                return <div>
                    <input ref={el => this.input = el} 
                        type="text"  
                        onFocus={this.focus}  
                        onBlur={this.blur}
                        onKeyDown={this.keydown}
                        /> <button>发送</button>
                    <hr />
                    <div ref={el => this.result = el} id="result">
                        <ul>
                            {
                                this.state.msgs.map((item, index) => {
                                    return <li key={index}>{item}</li>
                                })
                            }
                        </ul>
                    </div>
                </div>
            }

            focus = () => {
                // console.log('我获得了焦点')
                this.input.classList.add('active');
            }

            blur = (e) => {
                // console.log('我丧失了焦点');
                e.target.classList.remove('active');
            }

            keydown = (e) => {
                //判断用户按下的按键是否为 回车键   e  event
                // console.log(e.keyCode);
                if(e.keyCode === 13){
                    //将input框中的值, 展示在 div 中
                    let v = e.target.value;
                    //将 v 插入到 msgs 数组中
                    this.setState({
                        msgs:[
                            ...this.state.msgs,
                            v
                        ]
                    })
                    //设置result 标签的文本内容
                    // this.result.innerHTML = v;
                    //清空input的内容
                    e.target.value = '';
                }

            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>
</body>
</html>